//
// Created by denglibin on 2021/3/29.
// 渲染图片
#include <SDL.h>
#define WIN_WIDTH 960
#define WIN_HEIGHT 600
#define FRAME_RATE 60 //帧率
#define FONT_SIZE 32
static struct SDL_Window * win; //窗口
static struct SDL_Surface * winSurface; //窗口的surface
static struct SDL_Renderer * winRender;//窗口渲染器
static SDL_Texture  * cartoonTexture;//图片texture
static int w, h; // 图片texture的宽高
static int currentIndex = 0; //当前渲染的是第副图
static int frameIndex = 0;
static int size = 8; //图片共多少副图
static int speed = 20; //动画移动速度
static void draw(){
    //设置渲染器颜色
    SDL_SetRenderDrawColor(winRender, 255, 255, 255, 255);
    SDL_RenderClear(winRender); //用设置的颜色清除
    SDL_Rect srcRect = {currentIndex * w / size, 0,  w / size, h};
    double scale = 0.5; //缩放比例
    SDL_Rect distRect = {0, 0, (int)(srcRect.w * scale), (int)(srcRect.h * scale) };
    int r = SDL_RenderCopy(winRender, cartoonTexture, &srcRect, &distRect);
    if (r != 0){
        SDL_Log("Can not copy render %s\n", SDL_GetError());
        exit(1);
    }
    frameIndex++;
    if(frameIndex > FRAME_RATE / speed){
        frameIndex = 0;
        currentIndex ++;
    }

    if (currentIndex >= size){
        currentIndex = 0; //重新设置为0
    }
    //生效
    SDL_RenderPresent(winRender);

}
/**
 * 事件循环
 */
static void eventLoop(){
    SDL_Event  event;
    while (1)
    {//主消息循环
        SDL_PollEvent(&event);
        if(event.type == SDL_QUIT){
            return;
        }
        draw();
        uint32_t  begin = SDL_GetTicks();
        uint32_t  end = SDL_GetTicks();
        uint32_t  cost = end - begin;
        uint32_t per_frame_time = 1000 / FRAME_RATE;//每一帧耗时ms
        if (per_frame_time > cost){
            //减去循环体耗时，得到还应休眠的时间
            SDL_Delay(per_frame_time - cost);
        }
    }

}
/**
 * 初始化操作
 */
static void init(){
    //初始化，成功返回0
    if (SDL_Init(SDL_INIT_VIDEO)) {
        SDL_Log("Can not init video %s\n", SDL_GetError());
        exit(1);
    }
}
/**
 * 加载图片资源
 */
static void loadImage(){
    SDL_Surface  * surface = SDL_LoadBMP("img/catong.bmp");
    if (surface == NULL){
        SDL_Log("Can not load image %s\n", SDL_GetError());
        exit(1);
    }
    cartoonTexture = SDL_CreateTextureFromSurface(winRender, surface);
    if (cartoonTexture == NULL){
        SDL_Log("Can not create texture %s\n", SDL_GetError());
        exit(1);
    }
    //获取宽高
    SDL_QueryTexture(cartoonTexture, NULL, NULL, &w, &h);
    SDL_FreeSurface(surface);//清理

}



/**
 * 创建显示窗口
 * @return
 */
static int showWindow(){

    win = SDL_CreateWindow("Hello World",
                           SDL_WINDOWPOS_CENTERED,
                           SDL_WINDOWPOS_CENTERED,
                           WIN_WIDTH,
                           WIN_HEIGHT,
                           SDL_WINDOW_SHOWN
    );
    if (win == NULL) {
        SDL_Log("Can not create window %s\n", SDL_GetError());
        return 2;
    }

    winRender = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    winSurface = SDL_GetWindowSurface(win);
    return 0;

}

/**
 * 清理
 */
static void clean(){
    SDL_DestroyTexture(cartoonTexture);
    SDL_DestroyRenderer(winRender);
    SDL_FreeSurface(winSurface);
    SDL_DestroyWindow(win);
}
int main2_11(int argc, char* argv[]){
    init();
    if (showWindow()){
        exit(1);
    }
    loadImage();
    eventLoop();
    clean();
    SDL_Quit();
}


